﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;

namespace WS.EKA.Utilities.Extensions
{
    public static class HtmlExtenstion
    {
        public static MvcHtmlString MultipleSelectList(this HtmlHelper helper, string name)
        {
            return MultipleSelectList(helper, name, null);
        }
        public static MvcHtmlString MultipleSelectList(this HtmlHelper helper,
                string name, IEnumerable<SelectListItem> items)
        {
            var str = new StringBuilder();
            str.AppendFormat(@"<select name=""{0}"" multiple=""multiple"">", name);

            if (items != null && items.Count() > 0)
            {
                foreach (var item in items)
                {
                    str.Append(@"<option value='");
                    str.Append(item.Value);
                    str.Append("'");

                    if (item.Selected)
                        str.Append(@" selected=""selected""");
                    str.Append(" >");
                    str.Append(item.Text);
                    str.Append("</option>");
                }
            }

            str.Append("</select>");

            return MvcHtmlString.Create(str.ToString());
        }


        public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, bool inline)
        {
            return CheckBoxList(helper, name, null, inline);
        }
        public static MvcHtmlString CheckBoxList(this HtmlHelper helper,
                string name, IEnumerable<SelectListItem> items, bool inline)
        {
            var str = new StringBuilder();

            if (items != null && items.Count() > 0)
            {
                foreach (var item in items)
                {
                    string inlineClass = inline ? "inline" : string.Empty;
                    str.AppendFormat(@"<label class=""checkbox {0}""><input type=""checkbox"" name=""", inlineClass);
                    str.Append(name);
                    str.Append("\" value=\"");
                    str.Append(item.Value);
                    str.Append("\"");

                    if (item.Selected)
                        str.Append(@" checked=""chekced""");

                    str.Append(" />");
                    str.Append(item.Text);
                    str.Append("</label>");
                }
            }

            return MvcHtmlString.Create(str.ToString());
        }
        public static MvcHtmlString ImageActionLink(this HtmlHelper helper, string imgHtml, string actionName, string controllerName, object routeValues, object htmlAttributes)
        {
            UrlHelper urlHelper = ((Controller)helper.ViewContext.Controller).Url;
            string url = urlHelper.Action(actionName, controllerName, routeValues);

            TagBuilder imglink = new TagBuilder("a");
            imglink.MergeAttribute("href", url);
            imglink.InnerHtml = imgHtml;
            imglink.MergeAttributes(new RouteValueDictionary(htmlAttributes), true);

            return MvcHtmlString.Create(imglink.ToString());
        }
        public static MvcHtmlString ImageActionLink(this HtmlHelper helper, string imgHtml, string actionName, object routeValues, object htmlAttributes)
        {
            return ImageActionLink(helper, imgHtml, actionName, null, routeValues, htmlAttributes);
        }
        public static MvcHtmlString ImageActionLink(this HtmlHelper helper, string imgHtml, string actionName, object routeValues)
        {
            return ImageActionLink(helper, imgHtml, actionName, routeValues, null);
        }

        
    }
}
